package com.jl.basicframework.ui.customizeView;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.jl.basicframework.R;
import com.jl.basicframework.utils.dataProcessingUtils.FormatUtils;

import org.jetbrains.annotations.NotNull;

/***
 * @author 作者:姜澜
 * 时间:2022/3/11
 * 邮箱:316879104@pp.com
 * 类简述: 自定义菜单icam
 */
public class MenuItemView extends LinearLayout {

    /**
     * 图标
     */
    private ImageView iconView;

    /**
     * 标题
     */
    private TextView tileView;

    public MenuItemView(Context context) {
        super(context);
        bindView(context);
    }

    public MenuItemView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        bindView(context);
        initAttrs(context,attrs);
    }

    public MenuItemView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        bindView(context);
        initAttrs(context,attrs);
    }

    /**
     * 绑定控件
     */
    private void bindView(Context context){
        LayoutInflater.from(context).inflate(R.layout.view_menu_itm,this,true);
        iconView = findViewById(R.id.menuIcon);
        tileView = findViewById(R.id.menuTile);
    }

    /**
     * 初始化attrs
     */
    public void initAttrs(Context context,AttributeSet attrs){
        TypedArray mTypedArray = context.obtainStyledAttributes(attrs,R.styleable.MenuItemView);


        String tile = mTypedArray.getString(R.styleable.MenuItemView_tileText);
        if (FormatUtils.isStringNONull(tile)){
            tileView.setText(tile);
        }
        int textColor = mTypedArray.getColor(R.styleable.MenuItemView_tileColor, Color.BLACK);
        if (0!=textColor){
            tileView.setTextColor(textColor);
        }
        int textSize = (int) mTypedArray.getDimension(R.styleable.MenuItemView_tileSize,15);
        if (0!=textSize){
            tileView.setTextSize(textSize);
        }
        Drawable iconImage = mTypedArray.getDrawable(R.styleable.MenuItemView_iconImage);
        if (null!=iconImage){
            iconView.setImageDrawable(iconImage);
        }
        //回收
        mTypedArray.recycle();
    }

    /**
     * 设置图标
     */
    public MenuItemView setIcon(int mipmap){
        Glide.with(getContext()).load(mipmap).into(new SimpleTarget<Drawable>() {
            @Override
            public void onResourceReady(@NonNull @NotNull Drawable resource,
                                        @Nullable @org.jetbrains.annotations.Nullable Transition<? super Drawable> transition) {
                iconView.setImageDrawable(resource);
            }
        });
        return this;
    }

    /**
     * 设置标题
     */
    public MenuItemView setTile(String tile){
        tileView.setText(tile);
        return this;
    }

    /**
     * 设置标题颜色
     */
    public MenuItemView setTileColor(int color){
        tileView.setTextColor(getContext().getResources().getColor(color));
        return this;
    }

}
